metadata_legacy.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Metadata generation logic for legacy source distributions.
  2. """
  3. import logging
  4. import os
  5. from pip._internal.exceptions import InstallationError
  6. from pip._internal.utils.setuptools_build import make_setuptools_egg_info_args
  7. from pip._internal.utils.subprocess import call_subprocess
  8. from pip._internal.utils.temp_dir import TempDirectory
  9. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  10. if MYPY_CHECK_RUNNING:
  11. from pip._internal.build_env import BuildEnvironment
  12. logger = logging.getLogger(__name__)
  13. def _find_egg_info(directory):
  14. # type: (str) -> str
  15. """Find an .egg-info subdirectory in `directory`.
  16. """
  17. filenames = [
  18. f for f in os.listdir(directory) if f.endswith(".egg-info")
  19. ]
  20. if not filenames:
  21. raise InstallationError(
  22. "No .egg-info directory found in {}".format(directory)
  23. )
  24. if len(filenames) > 1:
  25. raise InstallationError(
  26. "More than one .egg-info directory found in {}".format(
  27. directory
  28. )
  29. )
  30. return os.path.join(directory, filenames[0])
  31. def generate_metadata(
  32. build_env, # type: BuildEnvironment
  33. setup_py_path, # type: str
  34. source_dir, # type: str
  35. isolated, # type: bool
  36. details, # type: str
  37. ):
  38. # type: (...) -> str
  39. """Generate metadata using setup.py-based defacto mechanisms.
  40. Returns the generated metadata directory.
  41. """
  42. logger.debug(
  43. 'Running setup.py (path:%s) egg_info for package %s',
  44. setup_py_path, details,
  45. )
  46. egg_info_dir = TempDirectory(
  47. kind="pip-egg-info", globally_managed=True
  48. ).path
  49. args = make_setuptools_egg_info_args(
  50. setup_py_path,
  51. egg_info_dir=egg_info_dir,
  52. no_user_config=isolated,
  53. )
  54. with build_env:
  55. call_subprocess(
  56. args,
  57. cwd=source_dir,
  58. command_desc='python setup.py egg_info',
  59. )
  60. # Return the .egg-info directory.
  61. return _find_egg_info(egg_info_dir)